<?php

namespace App\Observers;

use App\Models\Device;
use App\Models\LocationLog;
use App\Models\VisitLog;
use Carbon\Carbon;

class LocationLogObserver
{
    /**
     * Handle the LocationLog "created" event.
     *
     * @param \App\Models\LocationLog $model
     * @return void
     */
    public function created(LocationLog $model)
    {
        $device = Device::query()->where(['device_id' => $model->device_id])->first();
        $device->location_at = Carbon::now();
        $device->last_address = $model->address;
        $device->save();
    }

    /**
     * Handle the LocationLog "updated" event.
     *
     * @param \App\Models\LocationLog $model
     * @return void
     */
    public function updated(LocationLog $model)
    {
        //
    }

    /**
     * Handle the LocationLog "deleted" event.
     *
     * @param \App\Models\LocationLog $model
     * @return void
     */
    public function deleted(LocationLog $model)
    {
        //
    }

    /**
     * Handle the LocationLog "restored" event.
     *
     * @param \App\Models\LocationLog $model
     * @return void
     */
    public function restored(LocationLog $model)
    {
        //
    }

    /**
     * Handle the LocationLog "force deleted" event.
     *
     * @param \App\Models\LocationLog $model
     * @return void
     */
    public function forceDeleted(LocationLog $model)
    {
        //
    }

    public function creating(LocationLog $model)
    {

    }

    public function saving(LocationLog $model)
    {
        $device = Device::query()->where(['device_id' => $model->device_id])->first();
        $model->device_code = $device->code;

    }

    public function saved(LocationLog $model)
    {
        $device = Device::query()->where(['device_id' => $model->device_id])->first();
        $device->longitude = $model->longitude;
        $device->latitude = $model->latitude;
        $device->save();
    }
}
